package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetDistributorManageListReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.DistributorManageListVo;
import com.shop2cn.iapi.sqdistributor.service.GetDistributorManageListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetDistributorManageList {

	// java接口
	private static GetDistributorManageListReq getDistributorManageListReq;
	private static GetDistributorManageListCall getDistributorManageListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取分销员管理列表");
	}

	@Before
	public void caseUp() {
		getDistributorManageListReq = new GetDistributorManageListReq();
		getDistributorManageListCall = new GetDistributorManageListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDistributorManageListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {

		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		Long merchantId = SqDistributorService
				.getMasterShopId(getDistributorManageListReq.getMerchantId());

		if (merchantId == 0L)
			merchantId = getDistributorManageListReq.getMerchantId();

		List<DistributorRelation> actualResultList = distributorRelationWrapper
				.selectByMerchantId(merchantId);

		// 过滤主商户自身
		actualResultList = actualResultList
				.stream()
				.filter(relation -> relation.getDistributor_id().longValue() != relation
						.getMerchant_id().longValue())
				.collect(Collectors.toList());

		List<DistributorManageListVo> expectResultList = getDistributorManageListCall
				.getDistributorManageListResponse()
				.getDistributorManageListVoList();

		Logger.verifyEquals(expectResultList.size(), actualResultList.size(),
				"验证列表大小");

		for (int i = 0; i < expectResultList.size(); i++) {

			DistributorRelation actualResult = actualResultList.get(i);
			DistributorManageListVo expectResult = expectResultList.get(i);

			Logger.verifyEquals(expectResult.getId(), actualResult.getId(),
					"验证id");
			Logger.verifyEquals(expectResult.getDistributorId(),
					actualResult.getDistributor_id(), "验证distributorId");
			Logger.verifyEquals(expectResult.getDistributorName(),
					actualResult.getDistributor_name(), "验证distributorName");
			Logger.verifyEquals(expectResult.getDistributorPhone(),
					actualResult.getDistributor_phone(), "验证distributorPhone");
			Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
					actualResult.getDistributor_level_index(),
					"验证distributorLevelIndex");
			Logger.verifyEquals(expectResult.getDistributorStatus(),
					actualResult.getDistributor_status(), "验证distributorStatus");
			Logger.verifyEquals(expectResult.getParentDistributorId(),
					actualResult.getParent_distributor_id(),
					"验证parentDistributorId");
			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证merchantId");
			DataUtil.verifyDateEquals(expectResult.getAddTime(),
					actualResult.getAdd_time(), "验证addTime");
			Logger.verifyEquals(expectResult.getInviteCount(),
					actualResult.getInvite_count(), "验证inviteCount");
			Logger.verifyEquals(expectResult.getDirectSubordinateCount(),
					actualResult.getDirect_subordinate_count(),
					"验证directSubordinateCount");
			Logger.verifyEquals(expectResult.getParentDistributorName(),
					actualResult.getParent_distributor_name(),
					"验证parentDistributorName");
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetDistributorManageList_001() {
		Logger.start(true, "获取分销员管理列表,多条数据");

		try {

			getDistributorManageListReq.setMerchantId(500865146L);
			// getDistributorManageListReq.setDistributorId(distributorId);
			// getDistributorManageListReq.setDistributorLevelIndex(distributorLevelIndex);
			// getDistributorManageListReq.setDistributorPhone(distributorPhone);
			// getDistributorManageListReq.setParentDistributorId(parentDistributorId);
			// getDistributorManageListReq.setStartTime(startTime);
			// getDistributorManageListReq.setEndTime(endTime);
			getDistributorManageListReq.setPageIndex(1);
			getDistributorManageListReq.setPageSize(10);

			getDistributorManageListCall.setData(getDistributorManageListReq);
			getDistributorManageListCall.callService();

			Logger.verifyEquals(true,
					getDistributorManageListCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetDistributorManageList_002() {
		Logger.start(true, "子店铺获取分销员管理列表,多条数据");

		try {

			getDistributorManageListReq
					.setMerchantId(ShenqiUserSet.chainStoreGoodsOwnerId);
			// getDistributorManageListReq.setDistributorId(distributorId);
			// getDistributorManageListReq.setDistributorLevelIndex(distributorLevelIndex);
			// getDistributorManageListReq.setDistributorPhone(distributorPhone);
			// getDistributorManageListReq.setParentDistributorId(parentDistributorId);
			// getDistributorManageListReq.setStartTime(startTime);
			// getDistributorManageListReq.setEndTime(endTime);
			getDistributorManageListReq.setPageIndex(1);
			getDistributorManageListReq.setPageSize(10);

			getDistributorManageListCall.setData(getDistributorManageListReq);
			getDistributorManageListCall.callService();

			Logger.verifyEquals(true,
					getDistributorManageListCall.getSuccess(), "验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
